﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace Hope.Gallery.Core
{
    public class PictureResize
    {
        /// <summary>
        /// Create a new resized picture from existing one
        /// </summary>
        /// <param name="existingPicture"></param>
        /// <param name="newPicture"></param>
        public static void ResizePicture(string existingPicture, string newPicture, int width, int height)
        {
            byte[] myByte = System.IO.File.ReadAllBytes(existingPicture);
            Image i;

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(myByte, 0, myByte.Length);
                i = Image.FromStream(ms);
            }

            Image thumb = null;
            if (i.Width > i.Height)
            {
                thumb = i.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
            }
            else
            {
                thumb = i.GetThumbnailImage(height, width, () => false, IntPtr.Zero);
            }

            thumb.Save(newPicture);
        }
    }
}




 